/**
 * 
 */
package com.alstudio.module.xmpp.protocol.xmpp.parser.iq.useroption;

import com.alstudio.module.xmpp.protocol.xmpp.ALXmppEventListener;
import com.alstudio.module.xmpp.protocol.xmpp.packact.ALIQ;
import com.alstudio.module.xmpp.protocol.xmpp.parser.iq.ALBaseIQParser;
import com.alstudio.module.xmpp.protocol.xmpp.parser.iq.IQParseEventHandler.IQXmlParseEventCallback;
import com.alstudio.utils.formater.BooleanUtils;
import com.alstudio.utils.formater.numeric.ALNumericUtils;
import com.alstudio.utils.log.ALLog;
import com.alstudio.utils.notification.SystemSettings;

/**
 * 解析用户设置文件
 * 
 * @author alonso lee
 * 
 */
public class GetUserOptionParser extends ALBaseIQParser implements
		IQXmlParseEventCallback {

	private SystemSettings settings;

	@Override
	public void parseIQPackage(ALIQ packact, String stream,
			ALXmppEventListener listener) throws Exception {
		// TODO Auto-generated method stub
		code = 0;
		settings = null;
		mListener = listener;
		startParse(packact, stream, this);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.module.xmpp.protocol.xmpp.parser.iq.IQParseEventHandler.
	 * IQXmlParseEventCallback#processErrorStartTag(java.lang.String)
	 */
	@Override
	public void processErrorStartTag(String tag) {
		// TODO Auto-generated method stub
		if (tag.equals(ERROR_CODE_TAG)) {
			code = ALNumericUtils.parseInt(getAttValue("code"), 0);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.module.xmpp.protocol.xmpp.parser.iq.IQParseEventHandler.
	 * IQXmlParseEventCallback#processSetStartTag(java.lang.String)
	 */
	@Override
	public void processSetStartTag(String tag) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.module.xmpp.protocol.xmpp.parser.iq.IQParseEventHandler.
	 * IQXmlParseEventCallback#processGetStartTag(java.lang.String)
	 */
	@Override
	public void processGetStartTag(String tag) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.module.xmpp.protocol.xmpp.parser.iq.IQParseEventHandler.
	 * IQXmlParseEventCallback#processResultStartTag(java.lang.String)
	 */
	@Override
	public void processResultStartTag(String tag) {
		// TODO Auto-generated method stub
		if (tag.equals(ITEM_TAG)) {
			if(settings == null){
				settings = new SystemSettings();
			}
			String key = getAttValue("key");
			String value = getAttValue("val");
			
			boolean b = BooleanUtils.parseOnOf(value);
			String startTime = "";
			String endTime = "";
			if(key.equals("slience")){
				if(value.contains("|")){
					String[] vals = value.split("\\|");
					b = BooleanUtils.parseOnOf(vals[0]);
					startTime = vals[1];
					endTime = vals[2];
				}
			}

			if ("hiding".equals(key)) {
				settings.setHiding(b);
			} else if ("blockstrangercall".equals(key)) {
				settings.setBlockstrangercall(b);
			} else if ("blockstrangermsg".equals(key)) {
				settings.setBlockstrangermsg(b);
			} else if ("callnotify".equals(key)) {
				settings.setCallNoticeState(b);
			} else if ("sound".equals(key)) {
				settings.setSoundState(b);
			} else if ("vibrate".equals(key)) {
				settings.setVbrationState(b);
			} else if ("slience".equals(key)) {
				settings.setVbrationState(b);
				settings.setMuteStartTime(startTime);
				settings.setMuteEndTime(endTime);
				ALLog.d("起始时间 "+settings.getMuteStartTime()+" 结束时间 "+settings.getMuteEndTime());
			} 
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.module.xmpp.protocol.xmpp.parser.iq.IQParseEventHandler.
	 * IQXmlParseEventCallback#processEndTag(java.lang.String)
	 */
	@Override
	public void processEndTag(String tag) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.module.xmpp.protocol.xmpp.parser.iq.IQParseEventHandler.
	 * IQXmlParseEventCallback#processStartDocument()
	 */
	@Override
	public void processStartDocument() {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.module.xmpp.protocol.xmpp.parser.iq.IQParseEventHandler.
	 * IQXmlParseEventCallback#processEndDocument()
	 */
	@Override
	public void processEndDocument() {
		// TODO Auto-generated method stub
		onCallback();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.module.xmpp.protocol.xmpp.parser.iq.ALBaseIQParser#onCallback
	 * ()
	 */
	@Override
	public void onCallback() {
		// TODO Auto-generated method stub
		if(mListener != null){
			mListener.onGetUserOptEvent(code, settings);
		}
		ALLog.d("回调用户配置文件");
	}

}
